package com.ecy.quadratic.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.Comic;

import java.util.List;

public class ComicRankAdapter extends BaseAdapter {

    private List<Comic> mComicList;
    private Context mContext;

    public ComicRankAdapter(Context context, List<Comic> comicList) {
        mContext = context;
        mComicList = comicList;
    }

    @Override
    public int getCount() {
        return mComicList == null ? 0 : mComicList.size();
    }

    @Override
    public Comic getItem(int position) {
        if (mComicList != null) {
            return mComicList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_comic_rank, null);
            viewHolder = new ViewHolder();
            viewHolder.comicTitleTV = (TextView) convertView.findViewById(R.id.comic_title_tv);
            viewHolder.comicDescribeTV = (TextView) convertView.findViewById(R.id.comic_describe_tv);
            viewHolder.comicReaderNumTV = (TextView) convertView.findViewById(R.id.comic_readernum_tv);
            viewHolder.comicCoverIV = (ImageView) convertView.findViewById(R.id.comic_cover_iv);
            viewHolder.mRankTV = (TextView) convertView.findViewById(R.id.ranktv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Comic comic = getItem(position);
        viewHolder.mRankTV.setText("" + (position + 1));
        if (position < 3) {
            viewHolder.mRankTV.setBackgroundResource(R.drawable.rank_bg);
        } else {
            viewHolder.mRankTV.setBackgroundResource(R.drawable.rank_default_bg);
        }
        viewHolder.comicTitleTV.setText(comic.getTitle());
        if (TextUtils.isEmpty(comic.getDescription())) {
            viewHolder.comicDescribeTV.setText("暂无介绍");
        } else {
            viewHolder.comicDescribeTV.setText(comic.getDescription());
        }
        if (TextUtils.isEmpty(comic.getRecentsection())) {
            viewHolder.comicReaderNumTV.setText("");
        } else {
            viewHolder.comicReaderNumTV.setText("更新至" + comic.getRecentsection());
        }
        String img = !TextUtils.isEmpty(comic.getImg()) ? comic.getImg() : "http://image.samanlehua.com/mh/" + comic.getId() + ".jpg";
        Glide.with(mContext).load(img).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(R.drawable.cover_default).into(viewHolder.comicCoverIV);


        return convertView;
    }

    public class ViewHolder {
        public TextView comicTitleTV;
        public TextView comicDescribeTV;
        public TextView comicReaderNumTV;
        public ImageView comicCoverIV;
        public TextView mRankTV;
    }

}
